package com.eascs.ticket.ticket.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.common.util.ObjectUtil;
import com.eascs.ticket.common.service.BaseSevice;
import com.eascs.ticket.configuration.service.TicketAssociationTypeService;
import com.eascs.ticket.ticket.entity.TicketAssociation;

@Service
public class TicketAssociationService extends BaseSevice {
	@Inject
	private TicketChangeLogService ticketChangeLogService;

	@Inject
	private TicketAssociationTypeService ticketAssociationTypeService;

	public List<Object[]> getTicketAssociationsByTicketId(String ticketId) {
		return this.proxyDao.findObjectsList(
				"SELECT ta.id, ta.association_ticket_id,p.name,t.title,tt.name  ticket_type_name,t.due_date,v.title vtitle,t.operator_name,ta.association_type,v.id vid FROM ticket_association ta inner JOIN ticket t ON t.id=ta.association_ticket_id inner join project p on t.project_id=p.id inner join ticket_type tt on tt.code=t.ticket_type_code left join ticket v on t.version_ticket_id=v.id WHERE ta.ticket_id=? ORDER BY ta.create_date",
				ticketId);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void insertTicketAssociation(TicketAssociation ticketAssociation) {
		TicketAssociation ticketAssociationTo = new TicketAssociation();
		ObjectUtil.copySimpleProperties(ticketAssociation, ticketAssociationTo);
		ticketAssociationTo.setAssociationTicketId(ticketAssociation.getTicketId());
		ticketAssociationTo.setTicketId(ticketAssociation.getAssociationTicketId());
		ticketAssociationTo.setAssociationType(ticketAssociationTypeService.getTicketAssociationTypeTo(ticketAssociation.getAssociationType()).getId());

		ticketChangeLogService.insertTicketChangeLogWithObject(ticketAssociation.getTicketId(), "增加关联", ticketAssociation);
		ticketChangeLogService.insertTicketChangeLogWithObject(ticketAssociationTo.getTicketId(), "增加关联", ticketAssociationTo);

		this.proxyDao.insert(ticketAssociation);
		this.proxyDao.insert(ticketAssociationTo);
	}

	@Transactional(transactionManager = "ticketTransactionManager")
	public void deleteTicketAssociation(TicketAssociation deleteTicketAssociation) {
		TicketAssociation ticketAssociation = this.proxyDao.getByIdWithEntity(deleteTicketAssociation);
		TicketAssociation ticketAssociationTo = new TicketAssociation();
		ticketAssociationTo.setAssociationType(ticketAssociationTypeService.getTicketAssociationTypeTo(ticketAssociation.getAssociationType()).getId());
		ticketAssociationTo.setTicketId(ticketAssociation.getAssociationTicketId());
		ticketAssociationTo.setAssociationTicketId(ticketAssociation.getTicketId());
		ticketAssociationTo = this.proxyDao.findEntityWithAny(ticketAssociationTo);

		ticketChangeLogService.insertTicketChangeLogWithObject(ticketAssociation.getTicketId(), "删除关联", ticketAssociation);
		ticketChangeLogService.insertTicketChangeLogWithObject(ticketAssociationTo.getTicketId(), "删除关联", ticketAssociationTo);

		this.proxyDao.deleteByIdWithEntity(ticketAssociation);
		this.proxyDao.deleteByIdWithEntity(ticketAssociationTo);
	}

	public TicketAssociation getTicketAssociationById(String id) {
		return this.proxyDao.getById(TicketAssociation.class, id);
	}

}
